{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. backward对向量进行反向传播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.autograd import Variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[13., 13.]], grad_fn=<CopySlices>)\n"
     ]
    }
   ],
   "source": [
    "j = torch.zeros(2,2)\n",
    "k = torch.zeros(1,2)\n",
    "m = Variable(torch.FloatTensor([[2,3]]), requires_grad=True)\n",
    "# print(m, k)\n",
    "k[0,0] = m[0,0]**2 + 3*m[0,1]\n",
    "k[0,1] = m[0,1]**2 + 2*m[0,0]\n",
    "print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([4., 3.])\n",
      "tensor([2., 6.])\n"
     ]
    }
   ],
   "source": [
    "# dk1/dx1与dk1/dx2\n",
    "m.grad.data.zero_()\n",
    "k.backward(torch.FloatTensor([[1,0]]), retain_graph=True)   # retain_graph反向传播后不释放计算图\n",
    "j[0,:] = m.grad.data\n",
    "print(j[0,:])\n",
    "m.grad.data.zero_()\n",
    "# dk2/dx1 与dk2/dx2\n",
    "k.backward(torch.FloatTensor([[0,1]]), retain_graph=True)  # backward中参数[0,1]对应k的第几维度梯度的系数\n",
    "j[1,:] = m.grad.data\n",
    "print(j[1,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 0.]]) torch.Size([1, 2])\n"
     ]
    }
   ],
   "source": [
    "a = torch.FloatTensor([[1,0]])   \n",
    "print(a, a.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. a[None]可以增加一个维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([1, 2])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 1, 1, 2])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(a.shape)\n",
    "a[None, None].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.2]"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
